package com.pbq.pickerlib;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.pbq.pickerlib.wheel.TextWheelManager;

import java.util.ArrayList;

public class WheelTextSelector {
    private Context mContext;
    public static final String WHEEL_RESULT = "wheel_result";

    // Scrolling 标志位
    private boolean scrolling = false;
    private Bundle mBundle;
    private TextView mName;
    private Button mButtonOk;
    private Button mButtonCancel;
    private LinearLayout mArea;
    private TextWheelManager mManager;

    public WheelTextSelector(Context Context, Bundle Bundle) {
        this.mContext = Context;
        this.mBundle = Bundle;
    }

    // 创建一个包含自定义view的PopupWindow
    public PopupWindow makePopupWindow(String name, final TextWheelManager manager) {
        final PopupWindow window;
        window = new PopupWindow(mContext);
        mManager = manager;
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);

        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.view_wheel_selector, null,false);
        window.setContentView(contentView);

        mName = (TextView) contentView.findViewById(R.id.wheel_selector_name);
        mName.setText(name);
        mArea = (LinearLayout) contentView.findViewById(R.id.wheel_selector_area);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        params.weight = 1;
        manager.getWheelViews(params, mArea);
        // 点击事件处理
        mButtonOk = (Button) contentView.findViewById(R.id.wheel_select_ok);
        mButtonOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> list = manager.getResult();
                mBundle.putStringArrayList(WHEEL_RESULT, list);
                window.dismiss();
            }
        });

        mButtonCancel = (Button) contentView.findViewById(R.id.wheel_selector_cancel);
        mButtonCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });

        window.setWidth(Util.getWidthPx(mContext));
        window.setHeight(Util.getHeightPx(mContext) / 3);

        // 设置PopupWindow外部区域是否可触摸
        window.setFocusable(true); // 设置PopupWindow可获得焦点
        window.setTouchable(true); // 设置PopupWindow可触摸
        window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        window.setAnimationStyle(R.style.popwindow_anim_style);
        return window;
    }

    public TextWheelManager getmManager() {
        return mManager;
    }

    public void setCheckNum(int... checkNums) {
        if (mManager == null) {
            return;
        }
        mManager.setCheckNum(checkNums);
    }

    public void setCheckResult(String... checkStrs) {
        if (mManager == null) {
            return;
        }
        mManager.setCheckResult(checkStrs);
    }
}
